#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowTitle("QPushButton 学习");

    // 创建图标对象
    QIcon icon(":/img/good.png");
    // 设置图标
    ui->pushButton_target->setIcon(icon);
    // 设置图标的尺寸
    ui->pushButton_target->setIconSize(QSize(50, 50));

    ui->pushButton_up->setIcon(QIcon(":/img/up.png"));
    ui->pushButton_up->setIconSize(QSize(50, 50));

    ui->pushButton_down->setIcon(QIcon(":/img/down.png"));
    ui->pushButton_down->setIconSize(QSize(50, 50));


    ui->pushButton_left->setIcon(QIcon(":/img/right.png"));
    ui->pushButton_left->setIconSize(QSize(50, 50));


    ui->pushButton_right->setIcon(QIcon(":/img/left.png"));
    ui->pushButton_right->setIconSize(QSize(50, 50));

    // 设置快捷键 (默认就是连发)
//    ui->pushButton_up->setShortcut(QKeySequence("ctrl + w"));
//    ui->pushButton_down->setShortcut(QKeySequence("s"));
//    ui->pushButton_left->setShortcut(QKeySequence("a"));
//    ui->pushButton_right->setShortcut(QKeySequence("d"));

    // 还可以通过枚举来设置快捷键
    ui->pushButton_up->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_W));
    ui->pushButton_down->setShortcut(QKeySequence(Qt::Key_S));
    ui->pushButton_left->setShortcut(QKeySequence(Qt::Key_A));
    ui->pushButton_right->setShortcut(QKeySequence(Qt::Key_D));

    // 开启鼠标点击的连发功能（键盘的连发默认就是支持的）
    ui->pushButton_up->setAutoRepeat(true);
    ui->pushButton_down->setAutoRepeat(true);
    ui->pushButton_left->setAutoRepeat(true);
    ui->pushButton_right->setAutoRepeat(true);

    // QSS 后面会讲
//    button->setStyleSheet(
//           "QPushButton {"
//           "   border: none; "          // 隐藏边框
//           "   background-image: url(:/path/to/your/image.png); "  // 设置背景图像
//           "   background-repeat: no-repeat; "  // 防止图像重复
//           "   background-position: center; "   // 图像居中
//           "}"
//       );
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_up_clicked()
{
    // 获取到 target 的位置
    QRect rect = ui->pushButton_target->geometry();
    // 基于上次的位置，设置新的位置
    ui->pushButton_target->setGeometry(rect.x(), rect.y() - 5, rect.width(), rect.height());
}

void Widget::on_pushButton_left_clicked()
{
    // 获取到 target 的位置
    QRect rect = ui->pushButton_target->geometry();
    // 基于上次的位置，设置新的位置
    ui->pushButton_target->setGeometry(rect.x() - 5, rect.y(), rect.width(), rect.height());
}

void Widget::on_pushButton_right_clicked()
{
    // 获取到 target 的位置
    QRect rect = ui->pushButton_target->geometry();
    // 基于上次的位置，设置新的位置
    ui->pushButton_target->setGeometry(rect.x() + 5, rect.y(), rect.width(), rect.height());
}

void Widget::on_pushButton_down_clicked()
{
    // 获取到 target 的位置
    QRect rect = ui->pushButton_target->geometry();
    // 基于上次的位置，设置新的位置
    ui->pushButton_target->setGeometry(rect.x(), rect.y() + 5, rect.width(), rect.height());
}
